import Vue from 'vue';
import VueRouter from 'vue-router';
import routes from './routes';

//应用插件
Vue.use(VueRouter);

//将VueRouter.prototype的push和replace存储一份
const originPush = VueRouter.prototype.push;
const originReplace = VueRouter.prototype.replace;

//增强push，用于解决编程式路由导航重复跳转出错的问题
VueRouter.prototype.push = function (location, okCallback, errCallback) {
	if (okCallback === undefined && errCallback === undefined) {
		originPush.call(this, location).catch(() => {});
	} else {
		originPush.call(this, location, okCallback, errCallback);
	}
};

//增强replace，用于解决编程式路由导航重复跳转出错的问题
VueRouter.prototype.replace = function (location, okCallback, errCallback) {
	if (okCallback === undefined && errCallback === undefined) {
		originReplace.call(this, location).catch(() => {});
	} else {
		originReplace.call(this, location, okCallback, errCallback);
	}
};

//创建并暴露路由器
const router = new VueRouter({
	mode: 'history',
	routes,
});
export default router;
